package com.tnsw.noxnox.settopbox;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import com.tnsw.coreutils.AsyncManager;
import com.tnsw.noxnox.R;
import com.tnsw.noxnox.R.layout;
import com.tnsw.noxnox.R.menu;

import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore.Files;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class JWPlayerActivity extends Activity {

	private String tag = "JWPlayerActivity";
	private WebView webview;
	private AsyncManager asyncManager;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_jwplayer);
		
		this.asyncManager = AsyncManager.getInstance();
		this.webview = (WebView) this.findViewById(R.id.webview1);
		String playerStr = this.createPlayer();
		Log.d(tag, playerStr);
		this.asyncManager.appendNetworkLog(playerStr);
		
		this.webview.getSettings().setJavaScriptEnabled(true);
		this.webview.getSettings().setLoadsImagesAutomatically(true);
		this.webview.getSettings().setPluginsEnabled(true);
		this.webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
		this.webview.getSettings().setAllowFileAccess(true);
		
		this.webview.setWebViewClient(new JWWebClient());
		this.webview.loadData(playerStr, "text/html", null);
		
	}
	
	private String createPlayer() {
		String libLocation = "file://" + Environment.getExternalStorageDirectory().getAbsolutePath() + "/com.tnsw.noxnox/contents/jwplayer.html5.js";
		String videoUrl = "http://172.16.50.1:8080/examples/1.webm";
		//videoUrl = "file:///mnt/sdcard/com.tnsw.noxnox/contents/1366430866903.webm";
		String htmlData = this.getJWPlayerContent(libLocation, videoUrl, "file:///mnt/sdcard/com.tnsw.noxnox/contents/1366430868018.jpg", 640, 300);
		
		return(htmlData);
	}
	
	private String getJWPlayerContent(String libLocation, String videoUrl, String thumbnailUrl, int width, int height) {
		StringBuffer sb = new StringBuffer();
		
		try {
			InputStream is = this.getAssets().open("player.html");
			InputStreamReader ir = new InputStreamReader(is);
			BufferedReader br = new BufferedReader(ir);
			
			String line = null;
		    while ((line = br.readLine()) != null) {
		        sb.append(line + "\r\n");
		    }  
		    
		} catch (Exception e) {
			return(null);
		}
		
		String resultStr = sb.toString();
		resultStr = resultStr.replace("{jwplayer_lib}", libLocation);
		resultStr = resultStr.replace("{video_url}", videoUrl);
		resultStr = resultStr.replace("{thumbnail_url}", thumbnailUrl);
		resultStr = resultStr.replace("{width}", String.valueOf(width));
		resultStr = resultStr.replace("{height}", String.valueOf(height));
		
		return(resultStr);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.jwplayer, menu);
		return true;
	}

}
